package com.example.cloud.service.business.medical.impl;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.constants.StateConstants;
import com.example.cloud.dao.business.medical.ExecuteRecordRepository;
import com.example.cloud.dao.business.medical.TreatPlanSurgicalProjectRepository;
import com.example.cloud.dao.finance.ChargeDetailsRepository;
import com.example.cloud.model.business.medical.ExecuteRecord;
import com.example.cloud.model.business.medical.TreatPlanSurgicalProject;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.finance.ChargeDetails;
import com.example.cloud.model.member.Member;
import com.example.cloud.service.basic.info.MemberService;
import com.example.cloud.service.business.medical.TreatPlanSurgicalProjectService;
import com.example.cloud.utils.BusinessUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class TreatPlanSurgicalProjectServiceImpl implements TreatPlanSurgicalProjectService {

    private static final Logger logger = Logger.getLogger(TreatPlanSurgicalProjectServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ExecuteRecordRepository executeRecordRepository;

    @Autowired
    private ChargeDetailsRepository chargeDetailsRepository;

    @Autowired
    private TreatPlanSurgicalProjectRepository repository;

    @Override
    public TreatPlanSurgicalProject findOne(Long id) {
        return repository.findOne(id);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT)
    public void doSave(List<TreatPlanSurgicalProject> records) {
        for (TreatPlanSurgicalProject record : records) {
            record.setId(null);
            // 设置会员信息
            ChargeDetails details = new ChargeDetails();
            Member member = memberService.findByPatientId(record.getPatientId());
            if(member != null){
                record.setMemberId(member.getId());
                details.setMemberId(member.getId());
            }
            record.setTotalPrice(record.getPrice().multiply(new BigDecimal(record.getQuantity())));
            record.setCreationTime(null);
            BusinessUtil.appendRecord(record);
            record = repository.save(record);

            // 生成收费
            generateChargeDetails(record, details);
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void generateChargeDetails(TreatPlanSurgicalProject record, ChargeDetails details){
        details.setPatientId(record.getPatientId());
        details.setBusinessId(record.getId());
        details.setBusinessType(StateConstants.CHARGE_BUSINESS_TYPE_SURGICAL);
        details.setChargeType(StateConstants.CHARGE_CHARGE_TYPE_SURGICAL);
        details.setItemId(record.getProjectId());
        details.setItemName(record.getProjectName());
        details.setQuantity(record.getQuantity());
        details.setPrice(record.getPrice());
        details.setTotalPrice(record.getTotalPrice());
        details.setCreationTime(null);
        BusinessUtil.appendRecord(details);
        chargeDetailsRepository.save(details);
    }

    @Override
    @Transactional
    public Result doDeleteById(Long id) {
        Result result = new Result();
        result.setCode(CommonConstants.SUCCESS);
        TreatPlanSurgicalProject sp = repository.findOne(id);
        if(sp.getChargeStatus() == StateConstants.CHARGE_STATUS_YES){
            result.setCode(CommonConstants.FAILURE);
            result.setMsg("购买成功后不能删除");
            return result;
        }
        // 物理删除
        repository.delete(id);
        // 删除待收费
        chargeDetailsRepository.deleteByBusinessIdAndChargeType(id, StateConstants.CHARGE_CHARGE_TYPE_SURGICAL);
        return result;
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        repository.deleteByIdIn(ids);
    }

    @Override
    @Transactional
    public void doExecute(Long id, int status, String content,TreatPlanSurgicalProject record) {
        ExecuteRecord executeRecord = new ExecuteRecord();
        executeRecord.setPatientId(record.getPatientId());
        executeRecord.setComplaintId(record.getComplaintId());
        executeRecord.setOrderId(record.getId());
        executeRecord.setStatus(status);
        executeRecord.setTreatType(StateConstants.EXECUTE_TREAT_TYPE_ONE);
        executeRecord.setItemType(StateConstants.EXECUTE_ITEM_TYPE_PROJECT);
        executeRecord.setItemId(record.getProjectId());
        executeRecord.setItemName(record.getProjectName());
        executeRecord.setDoctorName(record.getCreatedBy());
        executeRecord.setSource(record.getProjectName());
        executeRecord.setTotalCount(record.getQuantity());
        executeRecord.setMedicalTime(record.getCreationTime());
        executeRecord.setExecuteContent(content);
        executeRecord.setCreationTime(null);
        BusinessUtil.appendRecord(executeRecord);
        executeRecordRepository.save(executeRecord);
        // 更新已执行次数
        if(status == StateConstants.EXECUTE_STATUS_YES){
            record.setExecute(record.getExecute() + 1);
            BusinessUtil.appendRecord(record);
            repository.save(record);
        }
    }
}
